/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PropertiesEdit.java
 *
 * Created on 21-jun-2011, 17:18:07
 */
package pfc.gui;

import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import pfc.comun.IDEpfc;
import pfc.interfaces.Salvable;

/**
 *
 * @author xandru
 */
public class PropertiesEdit extends Editor implements Salvable {

    /** Creates new form PropertiesEdit */
    PropertiesConfiguration properties;

    public PropertiesEdit(PropertiesConfiguration cf) {
        initComponents();
        properties = cf;

        loadProperties();


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTabla = new javax.swing.JTable();
        jbAplicar = new javax.swing.JButton();
        jbCerrar = new javax.swing.JButton();
        jbAddLine = new javax.swing.JButton();
        jbDelLine = new javax.swing.JButton();

        jTabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Propiedad", "Valor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTabla);

        jbAplicar.setText("Aplicar");
        jbAplicar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAplicarActionPerformed(evt);
            }
        });

        jbCerrar.setText("Cerrar");
        jbCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCerrarActionPerformed(evt);
            }
        });

        jbAddLine.setText("Añadir");
        jbAddLine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAddLineActionPerformed(evt);
            }
        });

        jbDelLine.setText("Borrar");
        jbDelLine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbDelLineActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(91, 91, 91)
                        .addComponent(jbAplicar)
                        .addGap(66, 66, 66)
                        .addComponent(jbCerrar))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jbDelLine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbAddLine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(29, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jbAplicar)
                            .addComponent(jbCerrar)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jbAddLine)
                        .addGap(18, 18, 18)
                        .addComponent(jbDelLine)))
                .addContainerGap(46, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jbDelLineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbDelLineActionPerformed
        DefaultTableModel t = (DefaultTableModel) jTabla.getModel();
        t.removeRow(jTabla.getSelectedRow());
        IDEpfc.app.getIDE().addChanged(this);
    }//GEN-LAST:event_jbDelLineActionPerformed

    private void jbAddLineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAddLineActionPerformed
        DefaultTableModel t = (DefaultTableModel) jTabla.getModel();
        t.addRow(new String[]{"", ""});
        IDEpfc.app.getIDE().addChanged(this);
    }//GEN-LAST:event_jbAddLineActionPerformed

    private void jbCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCerrarActionPerformed
        this.setVisible(false);
        this.getParent().remove(this);
        IDEpfc.app.getIDE().removeChanged(this);
    }//GEN-LAST:event_jbCerrarActionPerformed

    private void jbAplicarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAplicarActionPerformed
        TableModel m = jTabla.getModel();
        properties.clear();
        if (m.getRowCount() > 0) {
            for (int row = 0; row < m.getRowCount(); row++) {
                String key = (String) m.getValueAt(row, 0);
                String value = (String) m.getValueAt(row, 1);
                if (key == null) {
                    continue;
                }

                properties.addProperty(key, value);
                System.out.println(properties + " " + key + " " + value);
            }
        }

        System.out.println("Guardando configuracion DB-source " + m.getRowCount());
        IDEpfc.logTime("Configuracion guardada " + properties.getBasePath());
        save();
    }//GEN-LAST:event_jbAplicarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTabla;
    private javax.swing.JButton jbAddLine;
    private javax.swing.JButton jbAplicar;
    private javax.swing.JButton jbCerrar;
    private javax.swing.JButton jbDelLine;
    // End of variables declaration//GEN-END:variables

    private void loadProperties() {
        jTabla.setModel(new javax.swing.table.DefaultTableModel(new String[][]{}, new String[]{"Propiedad", "Valor"}));
        DefaultTableModel m = (DefaultTableModel) jTabla.getModel();
        for (Iterator i = properties.getKeys(); i.hasNext();) {
            String key = (String) i.next();
            String value = (String) properties.getProperty(key);
            m.addRow(new String[]{key, value});
        }
    }

    public void save() {
        try {
            properties.save();
        } catch (ConfigurationException ex) {
            Logger.getLogger(PropertiesEdit.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Error apacheconfig save" + ex.getMessage());
        }
    }
}
